package com.coocaa.ops.common.tool.string;

import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author bijiahao
 * @date : 2019/6/26.
 */
public class StringUtil {
    private static char[] numbersAndLetters = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    public static int countChineseWord(String text) {
        if (StringUtils.isBlank(text)) {
            return 0;
        }
        int count = 0;
        String regEx = "[\u4e00-\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(text);
        while (m.find()) {
            count++;
        }
        return count;
    }

    public static Integer getFirstUpperCharIndex(String packageName) {
        int len = packageName.length();
        int index = -1;
        for (int i = 0; i < len; ++i) {
            char c = packageName.charAt(i);
            if (Character.isUpperCase(c)) {
                index = i;
                return index;
            }
        }
        return index;
    }

    public static List<Integer> getUpperCharIndexList(String packageName) {
        int len = packageName.length();
        int index = -1;
        List<Integer> allIndex = InstanceUtil.newArrayList();
        for (int i = 0; i < len; ++i) {
            char c = packageName.charAt(i);
            if (Character.isUpperCase(c)) {
                index = i;
                allIndex.add(index);
            }
        }
        return allIndex;
    }

    public static String convertCamel2Path(String packageName) {
        if (isEmpty(packageName)) {
            return "";
        }


        List<Integer> allIndex = getUpperCharIndexList(packageName);
        if (null == allIndex || allIndex.size() == 0) {
            return packageName;
        }
        StringBuilder sb = new StringBuilder();
        int start = 0;
        for (Integer index : allIndex) {
            String temp = packageName.substring(start, index);
            start = index;
            sb.append(temp.toLowerCase()).append("/");
        }
        sb.append(packageName.substring(start, packageName.length()).toLowerCase());
        return sb.toString();
    }


    public static String underlineToCamel(String param) {
        if (isEmpty(param)) {
            return "";
        } else {
            String temp = param.toLowerCase();
            int len = temp.length();
            StringBuilder sb = new StringBuilder(len);

            for (int i = 0; i < len; ++i) {
                char c = temp.charAt(i);
                if (c == '_') {
                    ++i;
                    if (i < len) {
                        sb.append(Character.toUpperCase(temp.charAt(i)));
                    }
                } else {
                    sb.append(c);
                }
            }

            return sb.toString();
        }
    }

    /**
     * 去除“\n”以及“\t”
     *
     * @param originStr
     * @return
     */
    public static String replaceCharEnterAndTab(String originStr) {
        if (StringUtils.isNotBlank(originStr)) {
            return originStr.replaceAll("\n|\r", "");
        }
        return originStr;
    }

    public static String trim(String str) {
        return null == str ? "" : str.trim();
    }

    public static String generateRandomStringByUuid(int length) {
        if (length >= 1 && length <= 32) {
            String uuid = UUID.randomUUID().toString().replace("-", "");
            char[] randBuffer = new char[length];
            int increment = uuid.length() / length;

            for (int i = 0; i < length; ++i) {
                String subStr = uuid.substring(i * increment, i * increment + increment);
                int hashCode = Math.abs(subStr.hashCode());
                int index = hashCode % numbersAndLetters.length;
                randBuffer[i] = numbersAndLetters[index];
            }

            return new String(randBuffer);
        } else {
            return null;
        }
    }

    public static String avoidNull(String str) {
        return str != null && !"null".equals(str) ? str : "";
    }

    public static String replaceBlank(String originStr, String blankReplaceStr) {
        return StringUtils.isBlank(originStr) ? blankReplaceStr : originStr;
    }

    public static boolean isEmpty(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }
}
